package com.nondo.rdp.finance.endpoint.api.admin;

import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.Article;
import com.nondo.rdp.finance.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController("apiAdminArticle")
@RequestMapping("/api/admin/article")
public class ArticleEndpoint {
    @Autowired
    ArticleService articleService;

    @Autowired
    UserService userService;

    /**
     * 获取文章详情
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
    public View info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "获取数据成功");

        Article article = null;
        if (id != null) {
            article = articleService.findOne(id);
        } else {
            article = new Article();
        }

        view.setData(article);
        return view;
    }

    /**
     * 获取文章列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Article> query) {
        View<Page> view = new View(20000, "获取列表成功");

        Article article = query.getData();

        Page<Article> page = articleService.findAll(new Page<Article>(query.getPage(), query.getSize()), article);

        view.setData(page);

        return view;
    }

    /**
     * 添加文章
     *
     * @param request
     * @param response
     * @param article
     * @param result
     * @return
     */
    @RequestMapping("/add")
    public View add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Article article, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断是否存在type类型
            Article isExist = this.articleService.findByType(article.getType());
            if (isExist != null) {
                return View.ofError("类型已经存在，请重新填写");
            }

            this.articleService.save(article);
            view.setData(article);
        }
        return view;
    }

    /**
     * 更新文章
     *
     * @param request
     * @param response
     * @param article
     * @param result
     * @return
     */
    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Article article, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.articleService.save(article);
        }

        return view;
    }

    /**
     * 删除文章
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.articleService.delete(id);

        return view;
    }
}